<?php

namespace App\Http\Controllers\Saas;

use App\Http\Config\ErrorCode;
use App\Models\User\UserBase;
use Illuminate\Http\Request;

class BaseController
{
    public $is_su = false;
    public $AU = null;
    public $noCheckTokenAction = []; //不校验token的方法名，
    public $returnData = array('code' => 0, 'msg' => '请求成功', 'data' => '');

    public function __construct(Request $request)
    {
        /*--- start 跨域测试用 (待删除) ---*/
        header('Access-Control-Allow-Origin: *');                                                                 // 允许任意域名发起的跨域请求
        header('Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE');
        header('Access-Control-Allow-Headers: X-Requested-With,X_Requested_With');
        header('Access-Control-Allow-Headers:x-requested-with,content-type');
        /*--- end 跨域测试用---*/

        if ($request->getMethod() == "OPTIONS") {
            $this->returnData = ErrorCode::$admin_enum['fail'];
            return $this->returnData;
        }
        $this->returnData = ErrorCode::$admin_enum['success'];
        if(!in_array($request->route()->getActionMethod(), $this->noCheckTokenAction)) {
            $token_verify = $this->_validToken($request);
            if ($token_verify['code'] > 0) {
                return $this->returnData;
            }
            $this->is_su = self::isSuperUser($token_verify['data']);
            $this->AU = $token_verify['data'];
            $this->returnData['data'] = '';
        }
        if ($this->is_su) {
            return $this->returnData;
        }
    }

    /* 验证是否为超管 */
    public static function isSuperUser($userData)
    {
        if ($userData['id'] != 1) {
            return false;
        }
        return true;
    }

    /* 验证token有效性 */
    private function _validToken(Request $request)
    {
        if(empty($request->token)){
            $this->returnData = ErrorCode::$admin_enum['params_error'];
            $this->returnData['msg'] = "用户凭证不能为空";
            return $this->returnData;
        }
        $userBaseModel = new UserBase();
        $userData = $userBaseModel->getUserBasicBySessionID($request->token, true);
        if (!is_array($userData)) {
            $this->returnData = ErrorCode::$admin_enum['token_expire'];
            return $this->returnData;
        }
        $this->returnData['data'] = $userData;
        return $this->returnData;
    }

    //处理图片路径
    function processingPictures($url)
    {
        global $scf_data;
        if (!$url) {
            return $url;
        }
        //去除路径一个点的字符
        if (substr($url, 0, 1) == '.') {
            $url = substr($url, 1, (strlen($url) - 1));
        }
        if (substr($url, 0, 1) != '/' && substr($url, 0, 1) != 'h') {
            $url = '/' . $url;
        }
        if (strstr($url, "http://")) {
            $url = str_ireplace('http://', 'https://', $url);
        }
        if (!strstr($url, "https")) {
            if ($scf_data['IS_SCF'] == true) {
                $host = 'https://' . $scf_data['system']['bucketConfig']['bucket'] . '.cos.' . $scf_data['system']['bucketConfig']['region'] . '.myqcloud.com';
                $url = $host . $url;
            } else {
                $path = $_SERVER['DOCUMENT_ROOT'];//获取网站根目录
                if (strstr($path, 'public')) {
                    $url = $_SERVER["REQUEST_SCHEME"] . '://' . $_SERVER['SERVER_NAME'] . $url;
                } else {
                    $url = $_SERVER["REQUEST_SCHEME"] . '://' . $_SERVER['SERVER_NAME'] . '/public' . $url;
                }
            }
        }
        return $url;
    }

    //统一返回数据
    public function return_result($data, $text = '')
    {
        if (!is_array($data['msg']) && $text && strpos($data['msg'], "%s") !== false) {
            $data['msg'] = sprintf($data['msg'], $text);
        }
        return response()->json($data, 200, [], JSON_UNESCAPED_UNICODE);
    }
}
